home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1996 March
/
EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso
/
earcd
/
demo
/
rotplnnr.lha
/
RouteConv.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-23
|
12KB
|
468 lines
/* RouteParse.c -- Converts RoadRoute files to RoutePlanner format */
/* (C) 1994 Chris Lawrence */
char __stdiowin[]="CON:0/11//60/";
#include "RouteConv_rev.h"
#include <libraries/mui.h>
#include <proto/muimaster.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/icon.h>
#include <proto/intuition.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <reg.h>
#include "RoutePlanner.h"
#define COPYRIGHT VERS" ("DATE") -- Copyright 1994 Chris Lawrence"
APTR app=NULL, mainwindow=NULL;
struct DiskObject *disko=NULL;
int main(void);
ULONG FindCity(struct List *citylist,char *cityname);
BOOL Convert(char *cityname, char *roadname, char *outname, UWORD Units);
void ButtonBehave(APTR button, LONG notifyval);
void ButtonBehave(APTR button, LONG notifyval)
{
DoMethod((ULONG *)button,MUIM_Notify,MUIA_Pressed,FALSE,app,2,
MUIM_Application_ReturnID, notifyval);
}
void _STD_32767_killapp(void);
void _STD_32767_killapp(void)
{
// SAS/C Autotermination function
// This removes the application using MUI_DisposeObject. It is called
// when the program exits. Normal exits would also save the application
// object.
if(disko) FreeDiskObject(disko);
if(app) MUI_DisposeObject(app);
}
char *measurelabels[]={"Kilometers (km)","Miles (mi)",NULL};
#define QUIT MUIV_Application_ReturnID_Quit
#define OK 1
struct DataBaseInfo dbi;
int main(void)
{
APTR BT_Ok, BT_Quit, ST_Cities, ST_Roads, ST_Output, CY_Distance;
APTR POP_Cities, POP_Roads, POP_Output;
ULONG i, sigs;
BOOL going=TRUE;
char *cities, *roads, *output;
app=ApplicationObject,
MUIA_Application_Title, "RouteConv",
MUIA_Application_Version, "$VER: "VERS" ("DATE")",
MUIA_Application_Copyright, "Copyright (C) 1994 Chris Lawrence",
MUIA_Application_Author, "Chris Lawrence",
MUIA_Application_Description,"Conversion utility for RoutePlanner",
MUIA_Application_Base, "ROUTECONV",
MUIA_Application_SingleTask, FALSE,
MUIA_Application_DiskObject,
disko=GetDiskObjectNew("PROGDIR:RouteConv"),
MUIA_Application_HelpFile, "PROGDIR:RoutePlanner.guide",
MUIA_HelpNode, "RouteConv",
// MUIA_Application_Commands, COMMANDS,
SubWindow,
mainwindow=WindowObject,
MUIA_Window_Title, "RouteConv",
MUIA_Window_ID, 'RCNV',
MUIA_Window_NoMenus, TRUE,
MUIA_Window_ScreenTitle, COPYRIGHT,
WindowContents,
VGroup,
Child, ColGroup(2),
Child, KeyLabel2("City file",'f'),
Child, POP_Cities=PopaslObject,
MUIA_Popstring_String, ST_Cities=KeyString("Cities",256,'f'),
MUIA_Popstring_Button, PopButton(MUII_PopFile),
ASLFR_TitleText, "Choose city file...",
End,
Child, KeyLabel2("Route file",'r'),
Child, POP_Roads=PopaslObject,
MUIA_Popstring_String, ST_Roads=KeyString("Roads",256,'r'),
MUIA_Popstring_Button, PopButton(MUII_PopFile),
ASLFR_TitleText, "Choose route file...",
End,
Child, KeyLabel2("Output file",'o'),
Child, POP_Output=PopaslObject,
MUIA_Popstring_String, ST_Output=KeyString("USA.route",256,'o'),
MUIA_Popstring_Button, PopButton(MUII_PopFile),
ASLFR_TitleText, "Choose output file...",
ASLFR_DoSaveMode, TRUE,
End,
Child, KeyLabel1("Distances in",'d'),
Child, CY_Distance=KeyCycle(measurelabels,'d'),
End,
Child,HGroup,
MUIA_Group_SameSize, TRUE,
Child, BT_Ok=KeyButton("Convert",'c'),
Child, BT_Quit=KeyButton("Quit",'q'),
End,
End,
End,
End;
if(app)
{
DoMethod(mainwindow,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
app,2,MUIM_Application_ReturnID,QUIT);
ButtonBehave(BT_Ok, OK);
ButtonBehave(BT_Quit, QUIT);
DoMethod(mainwindow,MUIM_Window_SetCycleChain,ST_Cities,ST_Roads,
ST_Output,NULL);
set(CY_Distance,MUIA_Cycle_Active,1);
set(mainwindow,MUIA_Window_Open,TRUE);
get(mainwindow,MUIA_Window_Open,&i);
if(i)
{
while(going)
{
switch(DoMethod(app,MUIM_Application_Input,&sigs))
{
case 0:
break;
case MUIV_Application_ReturnID_Quit:
get(POP_Cities,MUIA_Popasl_Active,&i);
if(!i) get(POP_Roads,MUIA_Popasl_Active,&i);
if(!i) get(POP_Output,MUIA_Popasl_Active,&i);
if(i)
{
MUI_Request(app,mainwindow,0,NULL,"_Ok",
"Can't quit while ASL popups are open.");
}
else
going=FALSE;
break;
case OK:
get(ST_Cities,MUIA_String_Contents,&cities);
get(ST_Roads,MUIA_String_Contents,&roads);
get(ST_Output,MUIA_String_Contents,&output);
get(CY_Distance,MUIA_Cycle_Active,&i);
set(app,MUIA_Application_Sleep,TRUE);
if(!Convert(cities,roads,output,(UWORD)i))
{
MUI_Request(app,mainwindow,0L,NULL,"_Ok",
"An error occurred during conversion.");
}
set(app,MUIA_Application_Sleep,FALSE);
break;
}
if(sigs && going) Wait(sigs);
}
}
else
MUI_Request(app,NULL,0,NULL,"Ok","Unable to create main window.");
}
else
MUI_Request(NULL,NULL,0,"RouteConv","Ok","Unable to create application.");
}
BOOL Convert(char *cityname, char *roadname, char *outname, UWORD Units)
{
FILE *incity, *inroad, *out;
struct List cities, roads;
volatile int citycount=0,roadcount=0;
int i, citylen, roadlen, outlen, curpos;
APTR statuswindow, BT_Cancel, GAUGE_Total, TX_Message;
BPTR filelock;
struct FileInfoBlock *fib;
NewList(&cities);
NewList(&roads);
strcpy(dbi.dbi_Author,VERS);
strcpy(dbi.dbi_EMail,DATE);
fib=AllocDosObject(DOS_FIB,NULL);
if(!fib)
{
MUI_Request(app,mainwindow,0,NULL,"_Ok","Memory allocation failure.");
return FALSE;
}
filelock=Lock(cityname, ACCESS_READ);
if(!filelock)
{
MUI_Request(app,mainwindow,0,NULL,"_Ok",
"Unable to open cities file\n"MUIX_PH"%s",cityname);
FreeDosObject(DOS_FIB,fib);
}
Examine(filelock,fib);
citylen=fib->fib_Size;
UnLock(filelock);
filelock=Lock(roadname, ACCESS_READ);
if(!filelock)
{
MUI_Request(app,mainwindow,0,NULL,"_Ok",
"Unable to open roads file\n"MUIX_PH"%s",roadname);
FreeDosObject(DOS_FIB,fib);
}
Examine(filelock,fib);
roadlen=fib->fib_Size;
UnLock(filelock);
FreeDosObject(DOS_FIB,fib);
incity=fopen(cityname,"r");
if(!incity)
{
fprintf(stderr,"Error reading cities file %s\n",cityname);
return FALSE;
}
while(!feof(incity))
{
static struct CityNode *cn;
static char instr[256], *ptr;
cn=calloc(1,sizeof(struct CityNode));
if(!cn)
{
fprintf(stderr,"Can't allocate memory!\n");
fclose(incity);
return FALSE;
}
fgets(instr,255,incity);
if(feof(incity)) continue;
ptr=strchr(instr,'\n');
if(ptr) *ptr='\0';
ptr=strchr(instr,',');
if(ptr)
{
*ptr='\0';
strcpy(cn->cn_City.StateName,ptr+1);
}
strcpy(cn->cn_City.CityName,instr);
cn->cn_City.CityID=citycount;
cn->cn_City.Displayed=TRUE;
cn->cn_City.Intersection=FALSE;
cn->cn_City.CoordsExist=FALSE;
AddTail(&cities,(struct Node *)cn);
citycount++;
fprintf(stderr,"Converting cities... %4d\r",citycount);
}
fclose(incity);
inroad=fopen(roadname,"r");
if(!inroad)
{
fprintf(stderr,"Error reading roads file %s\n",roadname);
return FALSE;
}
fprintf(stderr,"\n");
while(!feof(inroad))
{
static char instr[256], *ptr, *inptr, *oldptr;
static char city1[50],city2[50],road[30];
static int distance,hours,minutes,citynum1,citynum2;
static struct RouteNode *rn;
rn=calloc(1,sizeof(struct RouteNode));
if(!rn)
{
fprintf(stderr,"Can't allocate memory!\n");
fclose(inroad);
return FALSE;
}
fgets(instr,255,inroad);
if(feof(inroad)) continue;
ptr=strchr(instr,'\n');
if(ptr) *ptr='\0';
ptr=strchr(instr,',');
oldptr=instr;
if(ptr)
{
*ptr='\0';
inptr=ptr+1;
}
strcpy(city1,instr);
ptr=strchr(inptr,',');
oldptr=inptr;
if(ptr)
{
*ptr='\0';
inptr=ptr+1;
}
strcpy(city2,oldptr);
ptr=strchr(inptr,',');
oldptr=inptr;
if(ptr)
{
*ptr='\0';
inptr=ptr+1;
}
distance=atol(oldptr);
ptr=strchr(inptr,':');
oldptr=inptr;
if(ptr)
{
*ptr='\0';
inptr=ptr+1;
}
hours=atol(oldptr);
ptr=strchr(inptr,',');
oldptr=inptr;
if(ptr)
{
*ptr='\0';
inptr=ptr+1;
}
minutes=atol(oldptr);
strcpy(road,inptr);
citynum1=FindCity(&cities,city1);
citynum2=FindCity(&cities,city2);
rn->rn_Route.RouteID=roadcount;
rn->rn_Route.CityID[0]=citynum1;
rn->rn_Route.CityID[1]=citynum2;
rn->rn_Route.Distance=distance;
rn->rn_Route.Speed=hours*60+minutes;
rn->rn_Route.SpeedCode=USE_TIME;
rn->rn_Route.Units=Units;
strcpy(rn->rn_Route.Hiway,road);
AddTail(&roads,(struct Node *)rn);
roadcount++;
fprintf(stderr,"Converting roads... %5d\r",roadcount);
}
fclose(inroad);
fprintf(stderr,"\n\n");
out=fopen(outname,"wb");
if(!out)
{
fprintf(stderr,"Error creating file %s!\n",outname);
return FALSE;
}
fprintf(out,"RPL2CITY");
fwrite(&citycount,sizeof(citycount),1,out);
i=1;
do {
static struct CityNode *cn;
cn=(struct CityNode *)RemHead(&cities);
//fprintf(infofile,"%4d %s, %s\n",cn->cn_City.CityID,cn->cn_City.CityName,
// cn->cn_City.StateName);
fwrite(&cn->cn_City,sizeof(struct CityData),1,out);
fprintf(stderr,"Writing cities... %4d\r",i);
i++;
} while(!IsListEmpty(&cities));
fprintf(stderr,"\n");
fprintf(out,"ROAD");
fwrite(&roadcount,sizeof(roadcount),1,out);
i=1;
do {
static struct RouteNode *rn;
rn=(struct RouteNode *)RemHead(&roads);
//fprintf(infofile,"%s [from %d to %d]\n",rn->rn_Route.Hiway,
// rn->rn_Route.CityID[0],rn->rn_Route.CityID[1]);
fwrite(&rn->rn_Route,sizeof(struct RouteData),1,out);
fprintf(stderr,"Writing roads... %5d\r",i);
i++;
} while(!IsListEmpty(&roads));
dbi.dbi_AuthorCode=(ULONG)~0;
dbi.dbi_Security=DoFunkyThing(citycount+roadcount+dbi.dbi_AuthorCode+
StringSum(dbi.dbi_Author)+StringSum(dbi.dbi_EMail));
fwrite(&dbi,sizeof(dbi),1,out);
fprintf(stderr,"\n\nDone... %d cities and %d roads converted.\n",citycount,roadcount);
fclose(out);
return TRUE;
}
ULONG FindCity(struct List *citylist,char *cityname)
{
struct Node *n;
ULONG found=(ULONG)~0;
BOOL stateused=FALSE;
char city[60],state[30]="",*ptr;
strcpy(city,cityname);
ptr=strchr(city,'/');
if(ptr)
{
*ptr='\0';
strcpy(state,ptr+1);
stateused=TRUE;
}
for(n=citylist->lh_Head;n && found==(ULONG)~0;n=n->ln_Succ)
{
struct CityData *cd=&((struct CityNode *)n)->cn_City;
if(stateused)
{
if(!stricmp(state,cd->StateName) && !stricmp(city,cd->CityName))
found=cd->CityID;
}
else
{
if(!stricmp(city,cd->CityName)) found=cd->CityID;
}
}
return(found);
}